/*******************************************************************************

This code file produces Table A6, "Bootstrap Estimates of Citywide Supply Response and Marginal Fiscal Cost, Offsite versus Onsite Inclusionary Units," and Figure A11, "It Costs More to Put Onsite Inclusionary Units in Higher-Rent Neighborhoods."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
* Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"
	do "$code/modules/bootstrap_programs.do"
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	* Offsite option abolished after 2008
	drop if yearpermit < 2009
	
********************************************************************************
* Alternative-specific coefficients logit
********************************************************************************	

	* Define outcomes (no inclusionary housing, offsite, onsite)
	
	gen mult_outcome = .
	replace mult_out = 0 if max(inclusionary_offsite,inclusionary_onsite) == 0
	replace mult_out = 1 if inclusionary_offsite == 1
	replace mult_out = 2 if inclusionary_onsite == 1
	
	* Define constraints from economic theory
	
	constraint define 1 [#2]:dtaxrate_onsite = 0
	constraint define 2 [#3]:dtaxrate_offsite = 0
	constraint define 3 [#3]:dtaxrate_onsite = [#2]:dtaxrate_offsite
	
	* Estimate model
	
	mlogit mult_outcome dtaxrate_onsite dtaxrate_offsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) constraints(1 2 3) baseoutcome(0)
	
	mat b = e(b)
	local coef_dtaxrate = b[1,46]
	di `coef_dtaxrate'
	
	* Compute probabilities for offsite/onsite
	
	predict pr1, pr outcome(1)
	predict pr2, pr outcome(2)
	
	* Compute thresholds for offsite/onsite
	
	predict xb1, xb outcome(1)
	predict xb2, xb outcome(2)
	
	gen thresh_offsite_pp = dtaxrate_offsite - xb1/`coef_dtaxrate'
	gen thresh_onsite_pp = dtaxrate_onsite - xb2/`coef_dtaxrate'
	
	gen thresh_offsite = (1/0.2)*((assesstot-assessland)/(sh_taxable*unitsres*1000000))*(dtaxrate_offsite - xb1/`coef_dtaxrate')
	gen thresh_onsite = (1/0.2)*((assesstot-assessland)/(sh_taxable*unitsres*1000000))*(dtaxrate_onsite - xb2/`coef_dtaxrate')
	
	gen meangrossrent = (12/(1000000*0.05))*exp(lmeangrossrent)
	
	* Make binscatter
	
	binscatter thresh_onsite thresh_offsite meangrossrent, ///
		xtitle("Capitalized Mean Rent of Block Group ($ Millions)") ytitle("Breakeven Cost Per Inclusionary Unit ($ Millions)") ///
		graphregion(lcolor(white)) legend(order(1 "Onsite" 2 "Offsite"))

	graph export "$figs/breakevens_by_rent_bg_offsite_vs_onsite.pdf", replace
	graph export "$figs_overleaf/breakevens_by_rent_bg_offsite_vs_onsite.pdf", replace
	
	* Obtain citywide supply responses / marginal fiscal costs for offsite/onsite
	
	xtset, clear
	bootstrap elast_offsite = r(elast_offsite) elast_onsite = r(elast_onsite), reps(100) seed(1234) cluster(nta) idcluster(nta_boot): boot_elasticity_mnl
	
	matrix mat_elast = r(table)
	
	local elast1_estimate = mat_elast[1,1]
	local elast1_stderr = mat_elast[2,1]
	local elast2_estimate = mat_elast[1,2]
	local elast2_stderr = mat_elast[2,2]
	
	insert_into_file using tableA6.csv, key(elast1_estimate) value(`elast1_estimate') format(%12.2f)
	insert_into_file using tableA6.csv, key(elast1_stderr) value(`elast1_stderr') format(%12.2f)
	insert_into_file using tableA6.csv, key(elast2_estimate) value(`elast2_estimate') format(%12.2f)
	insert_into_file using tableA6.csv, key(elast2_stderr) value(`elast2_stderr') format(%12.2f)
	
	xtset, clear
	bootstrap mfc_offsite = r(mfc_offsite) mfc_onsite = r(mfc_onsite), reps(100) seed(1234) cluster(nta) idcluster(nta_boot): boot_mcost_mnl
	
	matrix mat_mcost = r(table)
	
	local mcost1_estimate = mat_mcost[1,1]
	local mcost1_stderr = mat_mcost[2,1]
	local mcost2_estimate = mat_mcost[1,2]
	local mcost2_stderr = mat_mcost[2,2]
	
	insert_into_file using tableA6.csv, key(mcost1_estimate) value(`mcost1_estimate') format(%12.0fc)
	insert_into_file using tableA6.csv, key(mcost1_stderr) value(`mcost1_stderr') format(%12.0fc)
	insert_into_file using tableA6.csv, key(mcost2_estimate) value(`mcost2_estimate') format(%12.0fc)
	insert_into_file using tableA6.csv, key(mcost2_stderr) value(`mcost2_stderr') format(%12.0fc)
	
		
********************************************************************************
* Create table
********************************************************************************
	
	cat tableA6.csv

	cap erase "$tables/output/tableA6.tex"

	cd "$code/modules"
	capture table_from_tpl, t("$tables/templates/tableA6.tex") ///
							r("$tables/estimates/tableA6.csv") ///
							o("$tables/output/tableA6.tex") 
			
	capture table_from_tpl, t("$tables/templates/tableA6.tex") ///
							r("$tables/estimates/tableA6.csv") ///
							o("$tables_overleaf/tableA6.tex") 
							
	exit
